package net.wanho.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.wanho.common.OutStoreConstant;
import net.wanho.dto.OutStoreAddDTO;
import net.wanho.dto.OutStoreConfirmDTO;
import net.wanho.dto.OutStoreDTO;
import net.wanho.dto.OutStoreExportDTO;
import net.wanho.mapper.OutStoreMapper;
import net.wanho.mapper.ProductMapper;
import net.wanho.po.OutStore;
import net.wanho.po.Product;
import net.wanho.service.OutStoreService;
import net.wanho.vo.OutStoreExportVO;
import net.wanho.vo.OutStoreVO;
import net.wanho.vo.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @author 32033
 * @description 针对表【out_store(出库单表)】的数据库操作Service实现
 * @createDate 2025-01-11 17:01:23
 */
@Service
@RequiredArgsConstructor
public class OutStoreServiceImpl extends ServiceImpl<OutStoreMapper, OutStore>
        implements OutStoreService {

    private final ProductMapper productMapper;

    @Override
    public PageInfo<OutStoreVO> getOutStorePageList(Integer pageNum, Integer pageSize, OutStoreDTO outStoreDTO) {
        Page<OutStoreVO> page = new Page<>(pageNum, pageSize);
//        LambdaQueryWrapper<OutStoreVO> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(!ObjectUtils.isEmpty(outStoreDTO.getStoreId()), OutStoreVO::getStoreId, outStoreDTO.getStoreId());
//        wrapper.like(!ObjectUtils.isEmpty(outStoreDTO.getProductName()), OutStoreVO::getProductName, outStoreDTO.getProductName());
//        wrapper.ge(!ObjectUtils.isEmpty(outStoreDTO.getStartTime()), OutStoreVO::getCreateTime, outStoreDTO.getStartTime());
//        wrapper.le(!ObjectUtils.isEmpty(outStoreDTO.getEndTime()), OutStoreVO::getCreateTime, outStoreDTO.getEndTime());
//        wrapper.eq(!ObjectUtils.isEmpty(outStoreDTO.getIsOut()), OutStoreVO::getIsOut, outStoreDTO.getIsOut());
        QueryWrapper<OutStore> wrapper = new QueryWrapper<>();
        wrapper.eq(outStoreDTO.getStoreId() != null, "o.store_id", outStoreDTO.getStoreId())
                .like(StringUtils.hasText(outStoreDTO.getProductName()), "p.product_name", outStoreDTO.getProductName())
                .ge(StringUtils.hasText(outStoreDTO.getStartTime()), "o.create_time", outStoreDTO.getStartTime())
                .le(StringUtils.hasText(outStoreDTO.getEndTime()), "o.create_time", outStoreDTO.getEndTime())
                .eq(StringUtils.hasText(outStoreDTO.getIsOut()), "o.is_out", outStoreDTO.getIsOut());

//        Page<OutStoreVO> pageInfo = this.baseMapper.selectOutStorePage(page, wrapper);
        Page<OutStoreVO> pageInfo = this.baseMapper.selectOutStorePage2(page, wrapper);

        PageInfo<OutStoreVO> pageInfo2 = new PageInfo<>();
        pageInfo2.setResultList(page.getRecords());
        pageInfo2.setPageCount(page.getPages());
        pageInfo2.setTotalNum(page.getTotal());
        pageInfo2.setPageSize(page.getSize());
        pageInfo2.setPageNum(page.getCurrent());
        pageInfo2.setLimitIndex(page.offset());
        return pageInfo2;
    }

    @Override
    public void addOutStorePage(OutStoreAddDTO outStoreAddDTO) {

        Product product = productMapper.selectById(outStoreAddDTO.getProductId());
        if (product == null) {
            throw new RuntimeException("商品不存在");
        }
        if (product.getProductInvent() < outStoreAddDTO.getOutNum()) {
            throw new RuntimeException("商品库存不足");
        }

        OutStore outStore = new OutStore();

        outStore.setProductId(outStoreAddDTO.getProductId());
        outStore.setStoreId(outStoreAddDTO.getStoreId());
        outStore.setOutNum(outStoreAddDTO.getOutNum());
        outStore.setOutPrice(BigDecimal.valueOf(outStoreAddDTO.getSalePrice()));
        outStore.setCreateBy(outStoreAddDTO.getCreateBy());
        outStore.setCreateTime(new Date());  // 使用当前时间
        outStore.setIsOut("0");
        outStore.setTallyId(null);
        this.save(outStore);
    }

    @Override
    public void confirmOutStore(OutStoreConfirmDTO confirmDTO) {
        // 判断商品库存 是否充足
        Product product = productMapper.selectById(confirmDTO.getProductId());
        if (product.getProductInvent() < confirmDTO.getOutNum()) {
            throw new RuntimeException("商品库存不足");
        }

        // 修改库存
        product.setProductInvent(product.getProductInvent() - confirmDTO.getOutNum());
        product.setUpdateTime(new Date());
        product.setUpdateBy(confirmDTO.getCreateBy());
        productMapper.updateById(product);

        // 修改出库单状态
        OutStore outStore = this.getById(confirmDTO.getOutsId());
        outStore.setIsOut(OutStoreConstant.OUT_STORE_IS_OUT_SUCCESS);
        outStore.setTallyId(confirmDTO.getCreateBy());
        this.updateById(outStore);

    }

    @Override
    public List<OutStoreExportVO> exportTable(Integer pageNum, Integer pageSize, OutStoreExportDTO exportDTO) {
        Page<OutStoreExportVO> page = new Page<>(pageNum, pageSize);
        QueryWrapper<OutStoreExportVO> wrapper = new QueryWrapper<>();
        wrapper.eq(exportDTO.getStoreId() != null, "o.store_id", exportDTO.getStoreId())
                .like(StringUtils.hasText(exportDTO.getProductName()), "p.product_name", exportDTO.getProductName())
                .ge(StringUtils.hasText(exportDTO.getStartTime()), "o.create_time", exportDTO.getStartTime())
                .le(StringUtils.hasText(exportDTO.getEndTime()), "o.create_time", exportDTO.getEndTime())
                .eq(StringUtils.hasText(exportDTO.getIsOut()), "o.is_out", exportDTO.getIsOut());

        List<OutStoreExportVO> pageResult = baseMapper.selectExportList(page, wrapper);

        pageResult.forEach(vo -> {
            vo.setStartTime(exportDTO.getStartTime());
            vo.setEndTime(exportDTO.getEndTime());
        });
        return pageResult;
    }
}




